import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:hbtrans_flutter/beans/perioddaily_bean.dart';
import 'package:hbtrans_flutter/utils/mydio.dart';
import 'package:hbtrans_flutter/utils/utils.dart';

import '../apis.dart';
import '../const.dart';
import '../routes.dart';

class PeriodDaily extends StatefulWidget {
  final stunum;
  final subject;
  final needtime;
  final lastinstancetime;

  const PeriodDaily(
      {Key key,
      this.stunum,
      this.subject,
      this.needtime = '',
      this.lastinstancetime = ''})
      : super(key: key);

  @override
  _PeriodDailyState createState() => _PeriodDailyState();
}

class _PeriodDailyState extends State<PeriodDaily> {
  var loadingDialog;
  List<PerioddailyBean> items = List();
  var classType = ['实操', '课堂教学', '模拟器教学', '远程教学'];

  get sonOrganizId => null;

  _detail() async {
    print('stunum:${widget.stunum}');
    print('needtime:${widget.needtime}');
    print('lastinstancetime:${widget.lastinstancetime}');
    Map data = Map<String, dynamic>();
    data["stunum"] = widget.stunum;
    data["subject"] = widget.subject;
    MyDio.getInstance(url: Const.TRANS_SERVER_PATH).get(Apis.hbperiodDetail,
        (data) {
      print(data);
      if (loadingDialog != null) {
        loadingDialog = null;
        Navigator.pop(context);
      }

      items.clear();
      for (var s in data) {
        items.add(PerioddailyBean.fromJson(s));
      }

      setState(() {});
    }, params: data);
  }

  _setItems() {
    List<Widget> widgets = List();
    if (items.length == 0) {
      widgets.add(Container());
    } else {
      for (PerioddailyBean periodailyyBean in items) {
        widgets.add(Container(
          padding: EdgeInsets.fromLTRB(20, 15, 20, 0),
          color: Colors.white,
          margin: EdgeInsets.only(bottom: 5),
          child: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Image.asset(
                    'images/ic_time.png',
                    height: 17,
                    width: 17,
                  ),
                  SizedBox(
                    width: 5,
                  ),
                  Text(
                    periodailyyBean.trainingdate,
                    style: TextStyle(fontSize: 14, color: Color(0xff333333)),
                  )
                ],
              ),
              SizedBox(
                height: 3,
              ),
              Row(
                children: <Widget>[
                  Image.asset(
                    'images/ic_trans_time_s.png',
                    height: 17,
                    width: 17,
                  ),
                  SizedBox(
                    width: 5,
                  ),
                  Text(
                    '${periodailyyBean.starttime}~${periodailyyBean.endtime}',
                    style: TextStyle(fontSize: 12, color: Color(0xff333333)),
                  )
                ],
              ),
              SizedBox(
                height: 10,
              ),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 25),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Text(
                              '培训时长',
                              style: TextStyle(
                                  fontSize: 12, color: Color(0xff999999)),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              Utils.getInstance().convertToPeriodHour(
                                  periodailyyBean.duration),
                              style: TextStyle(
                                  fontSize: 12, color: Color(0xff333333)),
                            )
                          ],
                        ),
                        Row(
                          children: <Widget>[
                            Text(
                              '有效学时',
                              style: TextStyle(
                                  fontSize: 12, color: Color(0xff999999)),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              periodailyyBean.reviewstatus == 2
                                  ? Utils.getInstance().convertToPeriodHour(
                                      periodailyyBean.lastinstancetime)
                                  : '--',
                              style: TextStyle(
                                  fontSize: 12, color: Color(0xff333333)),
                            )
                          ],
                        ),
                        Row(
                          children: <Widget>[
                            Text(
                              '学时类型',
                              style: TextStyle(
                                  fontSize: 12, color: Color(0xff999999)),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              classType[
                                  int.parse(periodailyyBean.subjcode) - 1],
                              style: TextStyle(
                                  fontSize: 12, color: Color(0xff333333)),
                            )
                          ],
                        ),
                      ],
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            Text(
                              '教练车牌号',
                              style: TextStyle(
                                  fontSize: 12, color: Color(0xff999999)),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              periodailyyBean.carName == null
                                  ? ''
                                  : periodailyyBean.carName,
                              style: TextStyle(
                                  fontSize: 12, color: Color(0xff333333)),
                            )
                          ],
                        ),
                        Row(
                          children: <Widget>[
                            Text(
                              '教练员',
                              style: TextStyle(
                                  fontSize: 12, color: Color(0xff999999)),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            Text(
                              periodailyyBean.coachName,
                              style: TextStyle(
                                  fontSize: 12, color: Color(0xff333333)),
                            )
                          ],
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  MaterialButton(
                    elevation: 0,
                    minWidth: 40,
                    height: 20,
                    color: Color(0xff23AFDC),
                    child: Text(periodailyyBean.rated == 0 ? '评价教练员' : '已评价',
                        style: TextStyle(color: Colors.white, fontSize: 10)),
                    onPressed: () {
                      if (periodailyyBean.rated != 0) {
                        return;
                      }
                      String route = '${Routes.rate}';
                      Routes.router.navigateTo(context, route,
                          replace: false, transition: TransitionType.fadeIn);
                    },
                  ),
                ],
              ),
            ],
          ),
        ));
      }
    }
    return widgets;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _detail();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xff23AFDC),
        elevation: 0,
        title: Text('培训日志'),
        centerTitle: true,
      ),
      body: Column(
        children: <Widget>[
          Container(
            color: Colors.white,
            child: SizedBox(
              height: 126,
              child: Stack(
                children: <Widget>[
                  Container(
                    height: 66,
                    color: const Color(0xff23AFDC),
                  ),
                  Positioned(
                    left: 0,
                    right: 0,
                    top: 20,
                    bottom: 20,
                    child: Card(
                      color: Colors.white,
                      shape: const RoundedRectangleBorder(
                          borderRadius: BorderRadius.all(Radius.circular(18))),
                      elevation: 10,
                      margin: EdgeInsets.symmetric(horizontal: 15),
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 45),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Text(
                                  int.parse(widget.needtime) < 0
                                      ? '0学时0分钟'
                                      : _convertToPeriodHour(
                                          int.parse(widget.needtime)),
//                                  widget.needtime,

                                  style: TextStyle(
                                      color: const Color(0xFff5A623),
                                      fontSize: 14),
                                ),
                                Text(
                                  '待培训学时',
                                  style: TextStyle(
                                      color: const Color(0xFf333333),
                                      fontSize: 10),
                                ),
                              ],
                            ),
                            Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Text(
                                  _convertToPeriodHour(
                                      int.parse(widget.lastinstancetime)),
                                  style: TextStyle(
                                      color: const Color(0xFf23AFDC),
                                      fontSize: 14),
                                ),
                                Text(
                                  '累积合格培训学时',
                                  style: TextStyle(
                                      color: const Color(0xFf333333),
                                      fontSize: 10),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
          Expanded(
            child: ListView(
              children: _setItems(),
            ),
          ),
        ],
      ),
    );
  }

  _convertToPeriodHour(int Stime) {
    int hour = Stime ~/ 45;
    int minutes = Stime - hour * 45;
    if (Stime <= 0) {
      hour = 0;
      minutes = 0;
    } else {
      hour = Stime ~/ 45;
      minutes = Stime - hour * 45;
    }
    return '${hour}学时${minutes}分钟';
  }
}
